﻿Imports SFDL.LoaderExtensions
Imports System.IO
Imports SFDL.Core
Imports System.Text
Imports SFDL.Loader.My.Resources

Module SettingsHelper

    Friend _settings As New Settings
    Friend CONST_SETTINGSFILEPATH As String = String.Empty
    Private _log As NLog.Logger = NLog.LogManager.GetLogger("SettingsHelperModule")

    Friend ReadOnly Property GetSettingsPath As String
        Get
            Return CONST_SETTINGSFILEPATH
        End Get
    End Property

    Friend ReadOnly Property GetSettings As Settings
        Get
            Return _settings
        End Get
    End Property


    Public Function GetDefaultSpeedReport() As String

        Dim _rt As New StringBuilder

        _rt.AppendLine(String.Format("{0} %%SFDL_DESC%%", StringMessages.SpeedreportDefaultFileInfo))
        _rt.AppendLine(String.Format("{0} %%SFDL_UPPER%%", StringMessages.SpeedreportDefaultFileInfo))
        _rt.AppendLine("")
        _rt.AppendLine(StringMessages.SpeedreportDefaultSpeedMessage)
        _rt.AppendLine("")
        _rt.AppendLine(String.Format("{0} %%COMMENT%%", StringMessages.SpeedreportDefaultComment))

        Return _rt.ToString

    End Function

    Friend Sub LoadSettings()

        Dim _tools As New Tools

        _settings = New Settings

        CONST_SETTINGSFILEPATH = Path.Combine(Environment.GetEnvironmentVariable("appdata"), "SFDL.NET 2\settings.xml")

        If Directory.Exists(Path.GetDirectoryName(CONST_SETTINGSFILEPATH)) = False Then
            Directory.CreateDirectory(Path.GetDirectoryName(CONST_SETTINGSFILEPATH))
        End If

        If IO.File.Exists(CONST_SETTINGSFILEPATH) Then

            _settings = CType(_tools.XMLDeSerialize(_settings, CONST_SETTINGSFILEPATH), Settings)

            _log.Info("Einstellungen geladen!")

            If String.IsNullOrWhiteSpace(_settings.SpeedReportSettings.SpeedreportTemplate) Then
                _settings.SpeedReportSettings.SpeedreportTemplate = GetDefaultSpeedReport()
            End If

        Else

            'Default Werte laden

            _log.Info("Keine Einstellungen gefunden - Lade Default Einstellungen")

            _settings.DownloadDirectory = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Download")

            If Directory.Exists(Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(Application.ExecutablePath)), "Download")) = False Then
                Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Download"))
            End If

            _settings.ExistingFileHandling = ExistingFileHandling.ResumeFile
            _settings.ClicknLoadEnabled = True
            _settings.Send2Tray = True
            _settings.MaxDownloadThreads = 3
            _settings.MaxRetry = 3
            _settings.RetryWaitTime = 3
            _settings.MarkAllContainerFiles = True
            _settings.Language = My.Computer.Info.InstalledUICulture.TwoLetterISOLanguageName

            'Default Speedreport

            Dim _speedreport_settings As New SpeedreportSettings

            _speedreport_settings.SpeedreportTemplate = GetDefaultSpeedReport()

            _settings.SpeedReportSettings = _speedreport_settings

            'Default UnRAR Einstellungen

            Dim _unrar_settings As New UnRARSettings

            _unrar_settings.UnRARAfterDownload = True
            _unrar_settings.UnRARPasswordList = New List(Of String)

            _settings.UnRARSettings = _unrar_settings

            'Danach dann die Default Einstellungen gleich speichern

            _tools.XMLSerialize(_settings, CONST_SETTINGSFILEPATH)

            _log.Info("Default Einstellungen geladen!")

        End If

    End Sub

End Module
